package com.kantboot.business.gc.service.impl;

import com.kantboot.business.gc.domain.dto.BusGcCollectSearchUserDTO;
import com.kantboot.business.gc.domain.entity.BusGcCollect;
import com.kantboot.business.gc.domain.entity.BusGcGame;
import com.kantboot.business.gc.domain.entity.BusGcUserOpenGame;
import com.kantboot.business.gc.domain.vo.CollectAndOpenGameVO;
import com.kantboot.business.gc.repository.BusGcCollectRepository;
import com.kantboot.business.gc.repository.BusGcUserOpenGameRepository;
import com.kantboot.business.gc.service.IBusGcCollectService;
import com.kantboot.system.user.service.IUserSelfService;
import com.kantboot.util.core.param.PageParam;
import com.kantboot.util.core.result.PageResult;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;
@Slf4j
@Service
public class BusGcCollectServiceImpl implements IBusGcCollectService {

    @Resource
    private BusGcCollectRepository repository;

    @Resource
    private BusGcUserOpenGameRepository userOpenGameRepository;

    @Resource
    private IUserSelfService userSelfService;


    @Override
    public PageResult getBy(PageParam<BusGcCollectSearchUserDTO> pageParam) {
        return PageResult.of(repository.findBy(pageParam.getData(), pageParam.getPageable()));
    }

    @Override
    public CollectAndOpenGameVO queryAddCollect(Long gameId) {
        CollectAndOpenGameVO collectAndOpenGameVO = new CollectAndOpenGameVO();
        Long selfId = userSelfService.getSelfId();
        BusGcCollect collect = repository.findByUserIdAndGameId(selfId, gameId);
        if (collect != null) {
           collectAndOpenGameVO.setCollectStatus(true);
        }
        List<BusGcUserOpenGame> byUserIdAndGameId = userOpenGameRepository.findByUserIdAndGameId(selfId, gameId);
        if (byUserIdAndGameId.size() > 0) {
            collectAndOpenGameVO.setOpenStatus(true);
        }
        return collectAndOpenGameVO;
    }

    @Override
    public List<BusGcGame> queryAddCollectList(Integer page, Integer size) {
        Long selfId = userSelfService.getSelfId();
        return null;
    }

    @Override
    public Boolean AddCollect(Long gameId) {
        if (gameId == null) {
            return false;
        }
        System.out.println("gameId = " + gameId);
        Long selfId = userSelfService.getSelfId();
        BusGcCollect collect = repository.findByUserIdAndGameId(selfId, gameId);
        if (collect != null) {
            return false;
        }
        repository.save(new BusGcCollect().setUserId(selfId).setGameId(gameId));
        return true;
    }

    @Override
    public Boolean cancelCollect(Long gameId) {
        if (gameId == null) {
            return false;
        }

        Long selfId = userSelfService.getSelfId();
        BusGcCollect collect = repository.findByUserIdAndGameId(selfId, gameId);
        log.info("selfId = " + selfId);
        log.info("gameId = " + gameId);
        log.info("collect = " + collect);
        if (collect != null) {
            repository.delete(collect);
            return true;
        }
        return false;
    }

    @Override
    public PageResult getBodyData(PageParam<BusGcCollectSearchUserDTO> pageParam) {
        return PageResult.of(repository.getBodyDataByUserId(pageParam.getData(), pageParam.getPageable()));
    }

}
